/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.swing;

import java.awt.Dimension;
import javax.swing.JTextField;
import javax.swing.plaf.metal.MetalFileChooserUI;
import javax.swing.text.Document;

/**
 * Class {@code PreferredHeightJTextField} is a {@link JTextField} that always
 * keep its preferred size. This class is useful with layout made from boxes or
 * grids but is unnecessary with the
 * <a target="_blank" href="https://forms.dev.java.net/">forms</a> library from
 * <a target="_blank" href="https://jgoodies.dev.java.net/">jGoodies</a>.
 * <p>
 * This code is stolen from the textfield in {@link MetalFileChooserUI}.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.swing.PreferredHeightJTextField">Daniel Léonard</a>
 * @version $Revision$
 */
public class PreferredHeightJTextField extends JTextField {

//---------------------------
// Class variables
//---------------------------

   /** The serial version uid. */
   private static final long serialVersionUID = 409606731758381195L;


//---------------------------
// Constructors
//---------------------------

   /**
    * Constructor.
    * @see   JTextField#JTextField()
    */
   public PreferredHeightJTextField() {
      super();
      }

   /**
    * Constructor.
    * @see   JTextField#JTextField(java.lang.String)
    */
   public PreferredHeightJTextField(String text) {
      super(text);
      }

   /**
    * Constructor.
    * @see   JTextField#JTextField(int)
    */
   public PreferredHeightJTextField(int columns) {
      super(columns);
      }

   /**
    * Constructor.
    * @see   JTextField#JTextField(java.lang.String, int)
    */
   public PreferredHeightJTextField(String text, int columns) {
      super(text, columns);
      }

   /**
    * Constructor
    * @see   JTextField#JTextField(javax.swing.text.Document, java.lang.String, int)
    */
   public PreferredHeightJTextField(Document doc, String text, int columns) {
      super(doc, text, columns);
      }


//---------------------------
// Overridden methods from javax.swing.JComponent
//---------------------------

   /**
    * Gets the maximum size. The height is the height from the preferred size.
    * @return   the maximum size.
    */
   @Override
   public Dimension getMaximumSize() {
      Dimension preferred_size = this.getPreferredSize();
      int width = Short.MAX_VALUE;
      int height = preferred_size.height;
      Dimension maximum_size = new Dimension(width, height);
      return maximum_size;
      }

   }
